﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;
namespace IHISCETeim.BLL
{
    public class OMailLineController : BaseController
    {
        public static bool AddOMailLine(OMailLineInfo obj,List<OMailLineDetailInfo> listDetail)
        {
            try
            {
                int MLID = OMailLineDAL.AddOMailLine(obj);
                if (MLID == -1)
                    return false;
                foreach (OMailLineDetailInfo itm in listDetail)
                {
                    itm.MLID = MLID;
                    AddOMailLineDetail(itm);
                }
                UpdateOMailLineMaxDate(MLID);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool UpdateOMailLineMaxDate(int MLID)
        {
            return OMailLineDAL.UpdateOMailLineMaxDate(MLID);
        }
        public static bool DeleteOMailLine(int MLID)
        {
            return OMailLineDAL.DeleteOMailLine(MLID);
        }

        public static OMailLineInfo GetOMailLineById(int MLID)
        {
            return CBO.FillObject<OMailLineInfo>( OMailLineDAL.GetOMailLineById(MLID));
        }

        public static DataTable GetOMailLinePagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return OMailLineDAL.GetOMailLinePagination(whereSql, PageIndex, PageSize, Sort, ref TotalCount);
        }

        public static bool UpdateOMailLine(OMailLineInfo obj, List<OMailLineDetailInfo> listDetail)
        {

            OMailLineDAL.UpdateOMailLine(obj);

            try
            {               
                foreach (OMailLineDetailInfo itm in listDetail)
                {                   
                    UpdateOMailLineDetail(itm);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        private static int AddOMailLineDetail(OMailLineDetailInfo obj)
        {
            return OMailLineDAL.AddOMailLineDetail(obj);
        }
        public static bool UpdateOMailLineDetail(OMailLineDetailInfo obj)
        {
            return OMailLineDAL.UpdateOMailLineDetail(obj);
        }

        public static List<OMailLineDetailInfo> GetOMailLineDetailByMLID(int MLID)
        {
            return CBO.FillCollection<OMailLineDetailInfo>( OMailLineDAL.GetOMailLineDetailByMLID(MLID));
        }

        public static List<OMailLineTempleteInfo> GetAllOMailLineTemplete()
        {
            return CBO.FillCollection<OMailLineTempleteInfo>( OMailLineDAL.GetAllOMailLineTemplete());
        }
        public static bool UpdateOMailLineTemplete(OMailLineTempleteInfo obj)
        {
            return OMailLineDAL.UpdateOMailLineTemplete(obj);
        }

        #region 报表
        /// <summary>
        /// 按工位统计NG量报表
        /// </summary>
        /// <param name="StateDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public static DataTableCollection OMailLine_GetReport1(string StateDate, string EndDate)
        {
            return OMailLineDAL.OMailLine_GetReport1(StateDate, EndDate);
        }
        /// <summary>
        /// 按检查内容统计NG量报表
        /// </summary>
        /// <param name="StateDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public static DataTableCollection OMailLine_GetReport2(string StateDate, string EndDate)
        {
            return OMailLineDAL.OMailLine_GetReport2(StateDate, EndDate);
        }

        public static DataTable OMailLine_GetMacTypeReport(string StateDate, string EndDate)
        {
            return OMailLineDAL.OMailLine_GetMacTypeReport(StateDate, EndDate);
        }
        #endregion
    }


    public class OMailLineTempleteDistinctComparer : IEqualityComparer<OMailLineTempleteInfo>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(OMailLineTempleteInfo x, OMailLineTempleteInfo y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.GW == y.GW && x.GWName == y.GWName;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(OMailLineTempleteInfo product)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(product, null)) return 0;

            //Get hash code for the Name field if it is not null.
            int hashProductName = product.GWName == null ? 0 : product.GWName.GetHashCode();

            //Get hash code for the Code field.
            int hashProductCode = product.GW.GetHashCode();

            //Calculate the hash code for the product.
            return hashProductName ^ hashProductCode;
        }

    }

    public class OMailLineDetailInfoDistinctComparer : IEqualityComparer<OMailLineDetailInfo>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(OMailLineDetailInfo x, OMailLineDetailInfo y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.GW == y.GW && x.GWName == y.GWName;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(OMailLineDetailInfo product)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(product, null)) return 0;

            //Get hash code for the Name field if it is not null.
            int hashProductName = product.GWName == null ? 0 : product.GWName.GetHashCode();

            //Get hash code for the Code field.
            int hashProductCode = product.GW.GetHashCode();

            //Calculate the hash code for the product.
            return hashProductName ^ hashProductCode;
        }

    }

}
